//
//  MaInCollectionViewCell.m
//  UI_Project_A
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "MaInCollectionViewCell.h"
#import "TableViewCell.h"
#import "HandleData.h"
#import "ModelOfData.h"
#import "FlowNewModel.h"
#import <UIImageView+WebCache.h>


#define WIDTH self.frame.size.width


@interface MaInCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,retain)NSMutableArray *arrayTitleWithNew;
@property (nonatomic ,retain)NSMutableArray *arrayCell;
@property (nonatomic ,retain)NSMutableArray *arrliudongImage;
@property (nonatomic ,retain)NSMutableArray *arrLabel;
@property (nonatomic ,retain)UIPageControl *page;
@property (nonatomic ,retain)UIScrollView *scroll;
@property (nonatomic ,retain)NSTimer *timer;
@end

@implementation MaInCollectionViewCell





- (void)dealloc {

    [_tableview release];
    [_arrayCell release];
    [_arrayTitleWithNew release];
    [_arrliudongImage release];
    [_page release];
    [_scroll release];
    [_timer release];
    [super dealloc];
}





- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        [self dataWith];
       
    }


    return self;
}






#pragma mark - Data

- (void)dataWith {
    
    if (!self.arrayTitleWithNew) {
        self.arrayTitleWithNew = [NSMutableArray array];
    }
    
    [HandleData handleBlock:^(id result) {
    
        NSDictionary *dic = [result objectForKey:@"data"];
        
        NSArray *arrNew = [dic objectForKey:@"news"];
        
        for (NSDictionary *dicNew in arrNew) {
            
            ModelOfData *model = [[ModelOfData alloc]init];
            NSString * str = [dicNew objectForKey:@"title"];
            
            if (str) {
                [model setValuesForKeysWithDictionary:dicNew];
                [self.arrayTitleWithNew addObject:model];
                [model release];
             
            }
        }
       
        [self createTableviewWithFrame];
       [self dataWithFlowNew];
//        [self.tableview reloadData];
    }];
   
   
}



#pragma mark - 流动新闻
- (void)dataWithFlowNew {
    
    self.arrliudongImage = [NSMutableArray array];
    self.arrLabel = [NSMutableArray array];
    
    [HandleData handleFloewNewWithString:@"http://api.baiyue.baidu.com/sn/api/medianewslist?type=image&subtype=1&ln=200&an=10&wf=1&pd=newsplus&platform=andriod&ver=1" Block:^(id result) {
        
        
        NSDictionary * dicData = [result objectForKey:@"data"];
        
        NSArray *arr =  [dicData objectForKey:@"news"];
        
        for (NSDictionary *dic in arr) {
            
            if (self.arrLabel.count < 4) {
                [self.arrLabel addObject:[dic objectForKey:@"title"]];
                
                NSArray *arrimageurls = [dic objectForKey:@"imageurls"];
                
                for (NSDictionary *dicurl in arrimageurls) {
                    FlowNewModel *model = [[FlowNewModel alloc] init];
                    model.url = [dicurl objectForKey:@"url"];
                    [self.arrliudongImage addObject:model];
                    
                }
            }
        }
        [self createScrollView];
    }];
    
}

#pragma mark - 轮播图
- (void)createScrollView {

    
    
    if (self.arrliudongImage.count != 0) {
        
    
        [self.arrliudongImage addObject:self.arrliudongImage.firstObject];
        
        [self.arrliudongImage insertObject:[self.arrliudongImage objectAtIndex:self.arrliudongImage.count - 2]  atIndex:0];

        
        [self.arrLabel addObject:self.arrLabel [0]];
       
        [self.arrLabel insertObject:[self.arrLabel objectAtIndex:self.arrLabel.count - 2] atIndex:0];
    }
 
    
    
    
    UIView *viewScroll = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200)];
    viewScroll.backgroundColor = [UIColor blackColor];
    self.tableview.tableHeaderView = viewScroll;
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200)];
    [viewScroll addSubview:self.scroll];
    self.scroll.contentSize = CGSizeMake(self.frame.size.width *self.arrLabel.count, 0);
    self.scroll.backgroundColor = [UIColor lightGrayColor];
    self.scroll.pagingEnabled = YES;
    self.scroll.showsHorizontalScrollIndicator = NO;
    [self.scroll release];

    for (int i = 0; i < self.arrliudongImage.count; i++) {
       
        FlowNewModel *model = self.arrliudongImage[i];

        UIImageView *imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
        
        [self.scroll addSubview:imageview];
        [imageview release];
        imageview.frame = CGRectMake(self.frame.size.width *i, 0, self.frame.size.width, 200);
        [imageview sd_setImageWithURL:[NSURL URLWithString:model.url]];
        
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10 + self.frame.size.width * i, 150, self.frame.size.width - 100, 50)];
      [self.scroll addSubview:label];
       
        [label release];
        label.numberOfLines = 0;
        
    //    label.text = self.arrLabel[i];

    }
    
    
    self.scroll.contentOffset = CGPointMake(self.frame.size.width, 0);
    
    //代理
    self.scroll.delegate = self;
    
    
    
    
    //PageControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width - 80, 150, 40, 50)];
    [viewScroll  addSubview:self.page];
    [self.page release];
    //个数
    self.page.numberOfPages = self.arrliudongImage.count - 2;
    //点击事件
    [self.page addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventValueChanged];
    
    
    //定时器
    self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerChanger) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    self.timer = _timer;

}


#pragma mark - 定时器
- (void)timerChanger {


    if (self.scroll.contentOffset.x == WIDTH * 4) {
        
        self.page.currentPage = 0;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            int contentOfset = (int)self.scroll.contentOffset.x;
            self.scroll.contentOffset = CGPointMake(contentOfset + WIDTH, 0);
        }];
        
    }else {
        
        if (self.scroll.contentOffset.x == 0) {
            self.scroll.contentOffset = CGPointMake(WIDTH * 4, 0);
        }
        if (self.scroll.contentOffset.x == WIDTH * 5) {
            self.scroll.contentOffset = CGPointMake(WIDTH, 0);
        }
        
        self.page.currentPage = self.scroll.contentOffset.x / WIDTH;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            int contentOfset = (int)self.scroll.contentOffset.x;
            
            self.scroll.contentOffset = CGPointMake(contentOfset + WIDTH, 0);
        }];
    }


}




#pragma mark - Page点击事件


- (void)handleAction:(UIPageControl *)page {

    [self.scroll setContentOffset:CGPointMake(page.currentPage *self.frame.size.width, 0) animated:YES];
 


}





#pragma mark -- 当用户停止拖拽时, 调用定时器

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
   
    
}




#pragma mark - 轮播效果
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    /** 轮播效果 */
    if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width * (self.arrliudongImage.count - 2), 0)];
    }
    
    
    if (scrollView.contentOffset.x == self.frame.size.width * (self.arrliudongImage.count - 1)) {
        
        [scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
    }
    
    self.page.currentPage = self.scroll.contentOffset.x / self.frame.size.width -1 ;
   
}







#pragma mark - 添加每个Cell的tableview
- (void)createTableviewWithFrame{


//    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
self.tableview = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    
    
    [self addSubview:self.tableview];
    [self.tableview release];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;

    [self.tableview registerClass:[TableViewCell class] forCellReuseIdentifier:@"poolTableviewCell"];
    
    
    
    
    
}


#pragma mark - tableViewDatasouce

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrayTitleWithNew.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolTableviewCell"];

    ModelOfData *model = [self.arrayTitleWithNew objectAtIndex:indexPath.row];
    
    
    
    
    [cell passDataWithModel:model];
    
    


    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {




    return 100;
}


#pragma mark - TableView点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {





}







@end
